<?php
/**
 * Template Name: 自助友链页
 */
//自助友情CSS
wp_enqueue_style('blinks_style', trailingslashit(get_stylesheet_directory_uri()) . 'assets/css/pages/blinks.css?van_ver=' . VAN_VER);
if( isset($_POST['blink_form']) && $_POST['blink_form'] == 'send'){
    global $wpdb;

// 表单变量初始化
    $link_name = isset( $_POST['blink_name'] ) ? trim(htmlspecialchars($_POST['blink_name'], ENT_QUOTES)) : '';
    $link_url = isset( $_POST['blink_url'] ) ? trim(htmlspecialchars($_POST['blink_url'], ENT_QUOTES)) : '';
    $link_description = isset( $_POST['blink_dec'] ) ? trim(htmlspecialchars($_POST['blink_dec'], ENT_QUOTES)) : ''; // 网站描述
    $link_target = "_blank";
    $link_visible = "N"; // 表示链接默认不可见

// 表单项数据验证
    if ( empty($link_name) || mb_strlen($link_name) > 20 ){
        wp_die('连接名称必须填写，且长度不得超过20字');
    }

    if ( empty($link_url) || preg_match('/http[s]?:\/\/[\w.]+[\w\/]*[\w.]*\??[\w=&\+\%]*\.[a-zA-Z]+/is',$link_url) === 0) {
        wp_die('链接地址必须填写且必须是有效URL');
    }

    $sql_link = $wpdb->insert(
        $wpdb->links,
        array(
            'link_name' => '【待审核】--- '.$link_name,
            'link_url' => $link_url,
            'link_target' => $link_target,
            'link_description' => $link_description,
            'link_visible' => $link_visible
        )
    );

    wp_die('亲，友情链接提交成功，【等待站长审核中】！<p><a href="/">点此返回</a>', '提交成功');

}

get_header();

global $post;
$bg_img = _van("blinks")['blinks_img'] ;
$seo_title = get_the_title();
?>
  <div class="yfx-page-bg d-flex justify-content-center">
        <img src="<?php echo $bg_img;?>" alt="">
        <div class="yfx-content">
            <h3><?php echo _van("blinks")['blinks_title'] ?></h3>
            <p><?php echo _van("blinks")['blinks_ftitle'] ?></p>
        </div>
    </div>

  <div id="main">
    <div class="container">
      <div class="card-header bg-transparent" style="border-bottom:0px">
        <h3 class="mb-0" style="text-align: center;"></h3>
      </div>
      <div class="srcdict-yqlj">

        <div class="col-lg-6 col-12 hidden-sm">
            <?php if (have_posts()) : while (have_posts()) : the_post(); ?>
          <article class="col-md-10 mt20 col-md-offset-2 clearfix" style="margin-top: 20px;margin-bottom: 15px;padding: 1rem 1.5rem;">
              <?php if(function_exists('cmp_breadcrumbs')) cmp_breadcrumbs();?>
              <?php echo _van("blinks")['links_text'] ?>
        </article>
          <?php endwhile; else: ?>
          <?php endif; ?>
      </div>
        <div class="col-lg-6 col-12">
          <!--表单开始-->
          <form method="post" class="mt20" action="<?php echo $_SERVER["REQUEST_URI"]; ?>">

            <div class="form-group">
              <label for="blink_name"><font color="red">*</font> 链接名称:</label>
              <input type="text" size="40" value="" class="form-control" id="blink_name" placeholder="请输入链接名称" name="blink_name" />
            </div>

            <div class="form-group">
              <label for="blink_url"><font color="red">*</font> 链接地址:</label>
              <input type="text" size="40" value="" class="form-control" id="blink_url" placeholder="请输入链接地址" name="blink_url" />
            </div>

            <div class="form-group">
              <label for="blink_dec">网站描述:</label>
              <input  type="text" size="40" value="" class="form-control" id="blink_lianxi" placeholder="请输入网站简介" name="blink_dec" />
            </div>

            <div>
              <input type="hidden" value="send" name="blink_form" />
              <button type="submit" class="btn mybtn btn-primary">提交申请</button>
              <button type="reset" class="btn mybtn btn-warning mybtn-default">重填</button>
              （提示：带有<font color="red">*</font>，表示必填项~）
            </div>
          </form>
          <!--表单结束-->
        </div>
    </div>
  </div>
<?php get_footer(); ?>